package com.bjtime.designer.widget

import android.content.Context
import android.content.res.TypedArray
import android.graphics.Bitmap
import android.graphics.Rect
import android.graphics.drawable.BitmapDrawable
import android.support.v4.widget.NestedScrollView
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.AbsoluteLayout
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.Toast
import com.bjtime.designer.R
import com.bjtime.designer.attributes.BaseAttr
import com.bjtime.designer.attributes.ViewAttr
import com.bjtime.designer.utils
import com.bjtime.designer.view.RulerView
import kotlinx.android.synthetic.main.layout_designer_layout.view.*


class DesignerLayout : LinearLayout {

    var mLayout : View? = null
    var nShowRuler : Boolean = true
    var nVRulerSize : Int     = 40
    var nHRulerSize : Int     = 60

    var mHRulerView : RulerView
    var mVRulerView : RulerView

    var mDesignerLayout : AbsDesignerLayout

    constructor(context: Context) : this(context, null as AttributeSet)

    constructor(context: Context, attrs: AttributeSet) : this(context, attrs, 0)

    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        mLayout = LayoutInflater.from(context).inflate(R.layout.layout_designer_layout, null)

        if (attrs != null) {
            var a = context.obtainStyledAttributes(attrs, R.styleable.DesignerLayout)
            nShowRuler = a.getBoolean(R.styleable.DesignerLayout_rulerview, nShowRuler)
            nVRulerSize = a.getInt(R.styleable.DesignerLayout_vrulesize, nVRulerSize)
            nHRulerSize = a.getInt(R.styleable.DesignerLayout_hrulesize, nHRulerSize)
            a.recycle()
        }
        addView(mLayout)

        mDesignerLayout = findViewById(R.id.designerlayout1)
        mVRulerView = findViewById(R.id.designer_layout_staff_gauge_left)
        mHRulerView = findViewById(R.id.designer_layout_staff_gauge_top)
        if (!nShowRuler) {
            var params = mVRulerView.layoutParams
            params.width = 10
            mVRulerView.layoutParams = params

            var params1 = mHRulerView.layoutParams
            params1.height = 10
            mHRulerView.layoutParams = params

            // 操,美观,美观呀！
            setPadding(utils.dp2px(context, 10f),utils.dp2px(context, 10f), 0,utils.dp2px(context, 10f))
        }
        mHRulerView.setSize (nHRulerSize)
        mVRulerView.setSize (nVRulerSize)
        mHRulerView.bindRulerView(mVRulerView)


        var sview1 = findViewById<RelativeLayout>(R.id.sview1)
        sview1.addOnLayoutChangeListener { v, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom ->
            mVRulerView.setSpac(mHRulerView.getSpac())
            var layoutParams1 = mDesignerLayout.layoutParams as RelativeLayout.LayoutParams
            layoutParams1.height = (mHRulerView.getSpac() * mVRulerView.getSize()).toInt()
            mDesignerLayout.layoutParams = layoutParams1
            mDesignerLayout.setSpac(mHRulerView.getSpac().toInt())
        }
    }

    fun setSelectListener (l: AbsDesignerLayout.onViewSelect) {
        mDesignerLayout.setSelectListener(l)
    }


    fun setSize (w : Int, h : Int) {
        mHRulerView.setSize(w)
        mVRulerView.setSize(h)
    }

    fun addLabel (type : ViewAttr.VIEW_TYPE,content:String) {
        mDesignerLayout.addLabel(type,content)
    }

    fun getCount () : Int {
        return mDesignerLayout.childCount
    }

    fun getSelectEle () : BaseLabelView? {
        return mDesignerLayout.getSelectEle()
    }

    fun deleteEle (ele : BaseLabelView, isMovePrev : Boolean = false) {
        mDesignerLayout.deleteEle(ele, isMovePrev)
    }

    fun copyEle (ele : BaseLabelView) {
        var attr = ele.getAttr()
        attr.des_id = -1
        attr.des_left = attr.des_left + 30
        attr.des_top = attr.des_top + 30
        when (ele.des_viewtype) {
            ViewAttr.VIEW_TYPE.IMAGE    -> {
                if(ele is ImageView2) {
                    var b = ele.getAttr()
                    mDesignerLayout.addView(ImageView2(mDesignerLayout.context, b.des_image!!), attr)
                }
            }
            ViewAttr.VIEW_TYPE.TEXT     -> {
                if(ele is TextView2) {
                    var b = ele.getAttr()
                    mDesignerLayout.addView(TextView2(mDesignerLayout.context, b.des_text), attr)
                }
            }
            ViewAttr.VIEW_TYPE.LINE     -> mDesignerLayout.addView(LineView2    (mDesignerLayout.context), attr)
            ViewAttr.VIEW_TYPE.QRCODE   -> {
                if(ele is QRCodeView2){
                    var b=ele.getAttr()
                    mDesignerLayout.addView(QRCodeView2  (mDesignerLayout.context,b.des_text), attr)
                }
            }
            ViewAttr.VIEW_TYPE.BARCODE  -> {
                if(ele is QRCodeView2){
                    var b=ele.getAttr()
                    mDesignerLayout.addView(BarCodeView2  (mDesignerLayout.context,b.des_text), attr)
                }
            }
        }
    }

    fun setSize (v : BaseLabelView?, mRect : Rect) {
        if (v != null) {
            v.setRect (mRect)
            mDesignerLayout.update(v)
        }
    }

    fun getSize (v : BaseLabelView?) : Rect? {
        return Rect (v!!.des_left, v!!.des_top, v!!.des_width, v!!.des_height)
    }

    fun getAllLabel () : List<BaseAttr> {
        return mDesignerLayout.getAllLabel()
    }

    fun getAllLabelView () : List<BaseLabelView> {
        return mDesignerLayout.getAllLabelView()
    }

    fun clear () {
        mDesignerLayout.removeAllViews()
    }

    fun addView(child: BaseLabelView, attr:BaseAttr) {
        mDesignerLayout.addView(child, attr)
    }

    fun shot () : Bitmap? {
        return utils.viewShot (mDesignerLayout)
    }
}